<?php

namespace app\admin\model;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use support\Model;

/**
 * 管理员模型
 * @property int $id
 * @property string $user_name 管理员账号
 * @property string|null $mobile 手机号
 * @property string|null $email 邮箱
 * @property string $password 密码
 * @property string|null $avatar 头像
 * @property int $status 状态:0=禁用,1=启用
 * @property string|null $last_login_time 最后登录时间
 * @property string|null $last_login_ip 最后登录IP
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 */
class Admin extends Model
{
    /**
     * The table associated with the model.
     * @var string
     */
    protected $table = 'admin';
    /**
     * The primary key associated with the table.
     * @var string
     */
    protected $primaryKey = 'id';
    /**
     * Indicates if the model should be timestamped.
     * @var bool
     */
    public $timestamps = true;
    /**
     * The name of the "created at" column.
     * @var string|null
     */
    const CREATED_AT = 'create_time';
    /**
     * The name of the "updated at" column.
     * @var string|null
     */
    const UPDATED_AT = 'update_time';
    /**
     * The attributes that are mass assignable.
     * @var array
     */
    protected $fillable = [
        'user_name',
        'mobile',
        'email',
        'password',
        'avatar',
        'status',
        'last_login_time',
        'last_login_ip',
    ];
    /**
     * The attributes that should be cast to native types.
     * @var array
     */
    protected $casts = [
        'status' => 'integer',
        'last_login_time' => 'datetime:Y-m-d H:i:s',
        'create_time' => 'datetime:Y-m-d H:i:s',
        'update_time' => 'datetime:Y-m-d H:i:s',
    ];
    /**
     * The attributes that should be hidden for arrays.
     * @var array
     */
    protected $hidden = [
        'password',
    ];

    /**
     * 获取管理员的角色
     */
    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(Role::class, 'admin_role', 'admin_id', 'role_id');
    }
}
